package com.shixun.milktea.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.shixun.milktea.entity.Mt;
import com.shixun.milktea.entity.Sort;
import com.shixun.milktea.mapper.MtMapper;
import com.shixun.milktea.mapper.SortMapper;
import com.shixun.milktea.service.SortService;

import com.shixun.milktea.vo.PageVo;
import com.shixun.milktea.vo.ResultVO;
import com.shixun.milktea.vo.SortVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zrs
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/sort")
@Api(tags = "奶茶商品分类相关接口")
public class SortController {
    @Autowired
    SortService sortService;
    @Autowired
    SortMapper sortMapper;
    @Autowired
    MtMapper mtMapper;


    @GetMapping("/findAllSort")
    @ApiOperation(value = "查询所有分类")
    public ResultVO queryAllSort() throws Exception {

        List<Sort> allSort = sortMapper.findAllSort();

        return new ResultVO(allSort);
    }


    @GetMapping("/findAllSortById")
    @ApiOperation(value = "根据id查询分类")
    public ResultVO queryAllSortById(@RequestParam("sortId") int sortId) throws Exception {

        Sort sortById = sortMapper.findSortById(sortId);

        return new ResultVO(sortById);
    }


    @PostMapping("/addSort")
    @ApiOperation(value = "添加分类信息")
    public ResultVO addSort(@RequestBody Sort sort) throws Exception {

        Integer integer = sortMapper.addSort(sort);

        return new ResultVO(sort);
    }


    @PutMapping("/updateSort")
    @ApiOperation(value = "修改分类信息")
    public ResultVO updateSort(@RequestBody Sort sort) throws Exception {

        int i = sortMapper.updateSortById(sort);

        return new ResultVO(sort);
    }


    @DeleteMapping("/deleteSortById")
    @ApiOperation(value = "根据id删除分类信息及其分类下奶茶信息")
    public ResultVO deleteSortById(@RequestParam("sortId") int sortId) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("sort_id", sortId);
        int i = sortMapper.deleteById(sortId);
        mtMapper.deleteByMap(map);
        return new ResultVO(i);
    }


    @GetMapping("/querySortList")
    @ApiOperation(value = "分页查询分类信息")
    public ResultVO querySortList(PageVo pageVo) throws Exception {
        PageInfo sortPage = sortService.getSortPage(pageVo);
        return new ResultVO(sortPage);
    }
    @GetMapping("/querySortListAndMt")
    @ApiOperation(value = "查询分类信息及其奶茶信息")
    public ResultVO querySortListAndMt() throws Exception {
        List<Sort> allSortAndMt = sortService.getAllSortAndMt();
        return new ResultVO(allSortAndMt);
    }
}
